package com.qianyu.project.core.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.qianyu.project.core.bean.FlowNode;
import com.qianyu.project.core.bean.Form;
import com.qianyu.project.core.bean.FormAudit;
import com.qianyu.project.core.context.handler.FlowHandlerManager;
import com.qianyu.project.core.context.handler.FlowFinishedHandler;
import com.qianyu.project.core.context.user.FlowUserProvider;
import com.qianyu.project.core.mapper.FormAuditMapper;
import com.qianyu.project.core.service.IFlowNodeService;
import com.qianyu.project.core.service.IFormAuditService;
import com.qianyu.project.core.service.IFormService;
import com.qianyu.project.core.utils.FlowCheckUtil;
import com.qianyu.project.core.utils.FlowResultProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author gs
 * @date create in 2020/12/11 16:34
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FormAuditServiceImpl implements IFormAuditService {

    @Autowired
    private FormAuditMapper formAuditMapper;
    @Autowired
    private IFormService formService;
    @Autowired
    private IFlowNodeService flowNodeService;
    @Autowired
    private FlowHandlerManager flowHandlerManager;
    @Autowired
    private FlowUserProvider flowUserProvider;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(FormAudit formAudit) {
        //设置默认值
        formAudit.setCreatedate(DateUtil.now());
        formAudit.setEid(flowUserProvider.getFlowUser().getId());
        formAuditMapper.save(formAudit);
        //查询审批单
        Form form = formService.getById(formAudit.getFid());
        //拿到后置处理器
        FlowFinishedHandler flowFinishedHandler = flowHandlerManager.getHandler(form.getTarget());
        //判断是通过还是驳回
        if (FlowResultProvider.AUDIT_REFUSE_SIGN.equals(formAudit.getInfo())) {
            //修改单子状态为驳回
            form.setStatus(FlowResultProvider.FLOW_REFUSE_SIGN);
            formService.update(form);
            //验证处理器是否存在
            if (flowFinishedHandler != null) {
                //执行拒绝时的后置处理
                flowFinishedHandler.postRefuseHandle(
                        JSON.toJSONString(formService.renderTarget(form.getFid())),formAudit
                );
            }
        } else {
            //查找下一个节点
            FlowNode flowNode = flowNodeService.getById(formAudit.getFlowNodeId());
            FlowNode nextNode = flowNodeService.renderNextNode(flowNode);
            //判断是否存在 或 是否需要提前结束流程
            if (nextNode == null
                || FlowCheckUtil.check(form, flowNode, flowNodeService, formService)) {
                //证明已经完成流程
                form.setStatus(FlowResultProvider.FLOW_ALLOW_SIGN);
                //验证处理器是否存在
                if (flowFinishedHandler != null) {
                    //执行审批通过的后置处理
                    flowFinishedHandler.postAllowHandle(
                            JSON.toJSONString(formService.renderTarget(form.getFid())),formAudit
                    );
                }
            } else {
                //继续往下走流程
                form.setCurrent(nextNode.getFlowNodeId());
            }
        }
        formService.update(form);
    }

    @Override
    public List<FormAudit> getByForm(Integer fid) {
        return formAuditMapper.selectByForm(fid);
    }
}
